package cn.iocoder.yudao.module.shop.convert.itemconsume;

import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
import cn.iocoder.yudao.module.shop.controller.admin.itemconsume.ShopItemConsumeCreateReqVO;
import cn.iocoder.yudao.module.shop.controller.admin.itemconsume.ShopItemConsumeRespVO;
import cn.iocoder.yudao.module.shop.controller.admin.itemconsume.ShopItemConsumeUpdateReqVO;
import cn.iocoder.yudao.module.shop.controller.admin.store.vo.StoreRespVO;
import cn.iocoder.yudao.module.shop.dal.dataobject.itemconsume.ShopItemConsumeDO;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Mapper
public interface ShopItemConsumeConvert {
    ShopItemConsumeConvert INSTANCE = Mappers.getMapper(ShopItemConsumeConvert.class);

    default ShopItemConsumeDO convert(ShopItemConsumeCreateReqVO bean) {
        if ( bean == null ) {
            return null;
        }

        ShopItemConsumeDO.ShopItemConsumeDOBuilder builder = ShopItemConsumeDO.builder();
        builder.itemId(bean.getItemId());
        builder.count(bean.getCount());
        builder.storeId(bean.getStoreId());
        builder.tenantId(bean.getTenantId());

        return builder.build();
    }

    default ShopItemConsumeDO convert(ShopItemConsumeUpdateReqVO bean) {
        if(bean == null) {
            return null;
        }

        ShopItemConsumeDO.ShopItemConsumeDOBuilder builder = ShopItemConsumeDO.builder();
        builder.id(bean.getId());
        builder.storeId(bean.getStoreId());
        builder.count(bean.getCount());

        return builder.build();
    }

    default ShopItemConsumeRespVO convert(ShopItemConsumeDO shopItemConsumeDO){
        ShopItemConsumeRespVO vo = new ShopItemConsumeRespVO();
        vo.setId(shopItemConsumeDO.getId());
        vo.setItemId(shopItemConsumeDO.getItemId());
        vo.setCount(shopItemConsumeDO.getCount());
        vo.setStoreId(shopItemConsumeDO.getStoreId());

        return vo;
    }

    default List<ShopItemConsumeRespVO> convert(List<ShopItemConsumeDO> shopItemConsumeDOList, List<StoreRespVO> storeRespVOList) {
        Map<Long, StoreRespVO> storeRespVOMap = CollectionUtils.convertMap(storeRespVOList, StoreRespVO::getId);
        List<ShopItemConsumeRespVO> list = new ArrayList<>();
        shopItemConsumeDOList.forEach(shopItemConsumeDO -> {
            // 有可能消耗库存已经被删除
            if(storeRespVOMap.containsKey(shopItemConsumeDO.getStoreId()))
            {
                ShopItemConsumeRespVO shopItemConsumeRespVO = convert(shopItemConsumeDO);
                shopItemConsumeRespVO.setStoreItem(storeRespVOMap.get(shopItemConsumeRespVO.getStoreId()));
                list.add(shopItemConsumeRespVO);
            }
        });

        return list;
    }

    default ShopItemConsumeDO convert(Long itemId, Long storeId, Integer count){
        ShopItemConsumeDO consumeDO = new ShopItemConsumeDO();

        consumeDO.setItemId(itemId);
        consumeDO.setStoreId(storeId);
        consumeDO.setCount(count);

        return consumeDO;
    }
}
